import { Component, OnInit } from '@angular/core';
import { GridOptions } from '@ag-grid-community/core';
import { GridConfigService } from '@core/config/grid-config.service';
import { ApiService } from '@core/services/http/api.service';
import { UserAuthService } from '@core/services/user-auth.service';
import {ApesMessageService} from "@shared/apes/message";

@Component({
  selector: 'translate',
  templateUrl: './translate.component.html',
  styles: [`
    * {
      -webkit-overflow-scrolling: touch;
      -webkit-transform: translateZ(0);
    }
    ul {
      list-style:none;
    }
    ul > li {
      display: inline;
    }
    .span {
      color: red;
    }
  `]
})
export class TranslateComponent implements OnInit {
  /* 按钮相关 */
  buttonBar = [
    {hide: true, style: 'default', label: '刷新', id: 'action_query'}
  ];
  title = '';
  ctx = {model: {}};

  /** Grid 相关 **/
  resultData:any;
  gridOptions:GridOptions;
  pinnedBottomRowData:any[] = [];
  columnDefs:any[] = [];
  getContextMenuItems:any;
  height = 275 + 'px';
  posts :any ;                // 获取登录角色所在门店ID
  rowList:any = {};

  constructor(private apiService:ApiService,
              private gridConfigService:GridConfigService,
              private userAuthService: UserAuthService,
              private msg: ApesMessageService) {

    const userInfo = this.userAuthService.getUserInfoObj();
    if(userInfo.userAccount.post.branchObj !== undefined) {
      this.posts = userInfo.userAccount.post.branchObj.origin;
      // if(userInfo.userAccount.post.dept !== undefined) {
      //   this.posts = userInfo.userAccount.post.dept.id;
    }else {
      this.posts = null;
    }

    this.queryData();

    // 初始化 grid
    this.gridOptions = <GridOptions>{
      toolPanelSuppressSideButtons: true,
      suppressExcelExport: true,
      suppressCsvExport: true,
      suppressSizeToFit: true,
      showToolPanel: false,
      pagination: true,
      paginationPageSize: 6,
      rowHeight: 35
    };
    this.gridOptions.localeText = gridConfigService.getLocaleText();

  }


  ngOnInit() {
  }

  /**
   * 功能：按钮事件路由
   * @param data 当前按钮元数据
   */
  onEvent(data:any) {
    if (data instanceof MouseEvent) return;
    if (data) {
      if (data.id === 'action_query') {
        this.queryData();
      }
    }
  }

  queryData() {
    this.rowList = {};
    for(let i=0,len= this.reportList.length; i< len; i ++){
      this.reportList[i].rowData = [];
      let api = this.reportList[i].api;
      this.getTablesData(api, i);
    }
  }

  reportList = [
    {
      api: 'ipmMdzkForAppService.getTodayYX',
      title: '今日意向（含当日录入、意向跟踪）',
      rowData: [],
      titleList: [
        {headerName: "车牌", field: 'CPHM', width: 110},
        {headerName: "车型", field: 'CHXMC', width: 110},
        {headerName: "跟踪人", field: 'GZYGMC', width: 100},
        {headerName: "类型", field: 'GZTYPE', width: 150},
        {headerName: "备注", field: 'GZNOTE', width: 260},
        {headerName: "活跃度", field: 'VIPHYDMC', width: 100}
      ]
    },
    {
      api: 'ipmMdzkForAppService.getTodayDGZ',
      key: 2 ,
      title: '今日待跟踪',
      rowData: [],
      titleList: [
        {headerName: "车牌", field: 'CPHM', width: 110},
        {headerName: "跟踪人", field: 'GZYGMC', width: 110},
        {headerName: "保险到期", field: 'BXSL', width: 90},
        {headerName: "保养到期", field: 'BYSL', width: 90},
        {headerName: "未成交服务单", field: 'WCJFWDSL', width: 120},
        {headerName: "隐患项目关怀", field: 'WCJFWXM', width: 130},
        {headerName: "二手车跟踪", field: 'ESCSL', width: 100},
        {headerName: "报案跟踪", field: 'BASL', width: 90}
      ]
    },
    {
      api: 'ipmMdzkForAppService.getJRBXDQ',
      key: 3 ,
      title: '今日保险到店到期',
      rowData: [],
      titleList: [
        {headerName: "车牌", field: 'CPHM', width: 100},
        {headerName: "接车人", field: 'ZDR', width: 100},
        {headerName: "接车时间", field: 'ZDRQ_SJ', width: 100},
        {headerName: "商业险到期", field: 'SYXDQRQ_SJ', width: 100},
        {headerName: "交强险到期", field: 'JQXDRQ_SJ', width: 100},
        {headerName: "报价", field: 'BJZT', width: 65},
        {headerName: "订单", field: 'DDZT', width: 90},
        {headerName: "客户意向", field: 'KHYXH', width: 90},
        {headerName: "未成交原因", field: 'YXLR', width: 100}
      ]
    }
  ]

  getTablesData(api:string, i:any ) {
    let data = { 'interfaceMethod': api, 'pdata': { 'MDID': this.posts } };
    this.apiService.post('report.interface', {'body': JSON.stringify(data)}).subscribe(
      (resultData) => {
        const result = JSON.parse(resultData);
        if (result.success) {
          if (result.obj.length !== 0) {
            let data = JSON.parse(result.obj);
            this.rowList = {...this.rowList, ...data};
            if( data.array !== undefined ){
              for (let i = 0, len = data.array.length; i < len; i++) {
                data.array[i]['ZDRQ_SJ'] = this.timesHMS(data.array[i]['ZDRQ_SJ']);
                data.array[i]['SYXDQRQ_SJ'] = this.timesMD(data.array[i]['SYXDQRQ_SJ']);
                data.array[i]['JQXDRQ_SJ'] = this.timesMD(data.array[i]['JQXDRQ_SJ']);

                if (data.array[i]['BJZT'] === 1) {
                  data.array[i]['BJZT'] = '√';
                } else {
                  data.array[i]['BJZT'] = ' ';
                }

                // 今日带跟踪
                if( data.array[i]['BXSL'] ===1 ){ data.array[i]['BXSL'] = '√';
                }else{ data.array[i]['BXSL'] = ' '; }

                if( data.array[i]['BYSL'] ===1 ){ data.array[i]['BYSL'] = '√';
                }else{ data.array[i]['BYSL'] = ' '; }

                if( data.array[i]['WCJFWDSL'] ===1 ){ data.array[i]['WCJFWDSL'] = '√';
                }else{ data.array[i]['WCJFWDSL'] = ' '; }

                if( data.array[i]['WCJFWXM'] ===1 ){ data.array[i]['WCJFWXM'] = '√';
                }else{ data.array[i]['WCJFWXM'] = ' '; }

                if( data.array[i]['ESCSL'] ===1 ){ data.array[i]['ESCSL'] = '√';
                }else{ data.array[i]['ESCSL'] = ' '; }

                if( data.array[i]['BASL'] ===1 ){ data.array[i]['BASL'] = '√';
                }else{ data.array[i]['BASL'] = ' '; }

              }
            }


            if(data.array === undefined){
              this.reportList[i].rowData = data;
            }else {
              this.reportList[i].rowData = data.array;
            }
          }
        } else {
          this.msg.info(result.msg);
        }
      });
  }

  // 截取 时、分、秒
  timesHMS(key:string ) {
    if( key !== undefined ){
      key = key.slice(11, 19);
    }
    return key;
  }

  // 截取 月、日
  timesMD(key:string ) {
    if( key !== undefined ){
      key = key.slice(0, 10);
    }
    return key;
  }

}
